<template>
  <div class="account-box">
    <!-- 登录方式切换 -->
    <div class="toggle">
      <a href="javascript:;" @click="isMsgLogin =false" v-if="isMsgLogin">
        <i class="iconfont icon-user"></i> 使用账号登录
      </a>
      <a href="javascript:;" @click="isMsgLogin =true " v-else>
        <i class="iconfont icon-msg"></i> 使用短信登录
      </a>
    </div>

    <Form class="form" ref="target" :validation-schema="schema" v-slot="{errors}">
      <!-- 使用账号登录 -->
      <template v-if="!isMsgLogin">
        <div class="form-item">
          <div class="input">
            <i class="iconfont icon-user"></i>
            <Field v-model="formData.account" type="text" name="account" placeholder="请输入手机号"  :class="{error:errors.account}" />
          </div>
         <!-- 约定显示校验错误结果 -->
        <div class="error" v-if="errors.account"><i class="iconfont icon-warning" />{{errors.account}}</div>
        </div>
        <div class="form-item">
          <div class="input">
            <i class="iconfont icon-lock"></i>
            <Field v-model="formData.password" name="password" type="password" placeholder="请输入密码"  :class="{error:errors.password}"/>
            <div class="error" v-if="errors.password"><i class="iconfont icon-warning" />{{errors.password}}</div>
          </div>
        </div>
      </template>

      <!-- 使用验证码登录 -->
      <template v-else>
        <div class="form-item">
          <div class="input">
            <i class="iconfont icon-user"></i>
            <Field  v-model="formData.mobile" type="text" name="mobile" placeholder="请输入手机号"  :class="{error:errors.mobile}" />
            <!-- 约定显示校验错误结果 -->
           <div class="error" v-if="errors.mobile"><i class="iconfont icon-warning" />{{errors.mobile}}</div>
          </div>
        </div>
        <div class="form-item">
          <div class="input">
            <i class="iconfont icon-code"></i>
            <Field v-model="formData.code" name="code" type="text" placeholder="请输入验证码" :class="{error:errors.code}"/>
            <span @click="send" class="code">{{time===0?'发送验证码':`${time}秒后发送`}}</span>
          </div>
          <div class="error" v-if="errors.code"><i class="iconfont icon-warning" />{{errors.code}}</div>
        </div>
      </template>

      <div class="form-item">
        <div class="agree">
          <Field as="XtxCheckbox" name="isAgree" v-model="formData.isAgree" />
          <span>我已同意</span>
          <a href="javascript:;">《隐私条款》</a>
          <span>和</span>
          <a href="javascript:;">《服务条款》</a>
        </div>
        <!-- 约定显示校验错误结果 -->
        <div class="error" v-if="errors.isAgree"><i class="iconfont icon-warning" />{{errors.isAgree}}</div>
      </div>
      <a class="btn" @click="login" href="javascript:;" >登录</a>
    </Form>
    <div class="action">
      <!-- qq登录 -->
      <!-- <img src="https://qzonestyle.gtimg.cn/qzone/vas/opensns/res/img/Connect_logo_7.png" alt=""> -->
      <a href="https://graph.qq.com/oauth2.0/authorize?client_id=100556005&response_type=token&scope=all&redirect_uri=http%3A%2F%2Fwww.corho.com%3A8080%2F%23%2Flogin%2Fcallback">
        <img src="https://qzonestyle.gtimg.cn/qzone/vas/opensns/res/img/Connect_logo_7.png" alt="">
      </a>
      <div class="url">
        <a href="javascript:;">忘记密码</a>
        <a href="javascript:;">免费注册</a>
      </div>
    </div>
    <!-- <XtxMessage text="手机号或密码错误" type="error" /> -->
  </div>
</template>
<script>
import { useStore } from 'vuex'
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { Form, Field } from 'vee-validate'
import { useCountdown } from '@/compositions'
import { userMobileLoginMsg } from '@/api/user.js'
import { mobile, account, isAgree, password, code } from '@/utils/validata'
import Message from '@/components/Message'

export default {
  name: 'LongForm',
  components: { Form, Field },
  setup () {
    const store = useStore()
    const router = useRouter()
    const isMsgLogin = ref(false)
    const formData = reactive({
      account: 'yuan', // 名字
      mobile: '13241051259', // 手机号
      password: '123456', // 密码
      isAgree: true,
      code: ''// 验证码
    })
    // 校验规则
    const schema = {
      mobile: mobile,
      account: account,
      isAgree: isAgree,
      code: code
    }
    // 发手机验证码
    const send = async () => {
      // 1. 验证手机号格式
      if (mobile(formData.mobile) !== true) {
        Message({ type: 'error', text: '手机号格式不对' })
        return
      }
      if (time.value !== 0) {
        return
      }
      // 2.调用api
      try {
        await userMobileLoginMsg(formData.mobile)
        Message({ type: 'success', text: '验证码已经发送！' })
        start(60)
      } catch (err) {
        console.dir(err)
        Message({ type: 'error', text: err.response.data.message })
      }
    }
    const target = ref(null)
    // 兜底校验
    const login = () => {
      target.value.validate().then(res => {
        // console.log('兜底校验', res)
        if (!res) { return }

        isMsgLogin.value ? doUserMobileLongin() : doUserAccountLongin()

        // if (res) {
        //   if (isMsgLogin.value) {
        //     doUserMobileLongin()
        //   } else {
        //     doUserAccountLongin()
        //   }
        // }
      })
      console.log(formData)
    }
    // 账号登录
    const doUserAccountLongin = async () => {
      console.log('store.....', store)
      // 调用action
      const user = {
        account: formData.account,
        password: formData.password
      }
      try {
        // 需要在vuex的action主动抛出错误
        await store.dispatch('user/userAccountLogin', user)
        Message({ type: 'success', text: '登录成功' })
        router.push('/')
      } catch (err) {
        console.dir('错误对象')
        console.dir(err)
        Message({ type: 'error', text: err.message })
      }
    }
    // 手机验证登录
    const doUserMobileLongin = async () => {
      console.log('store.....', store)
      // 调用action
      const user = {
        mobile: formData.mobile,
        code: formData.code
      }
      try {
        // 需要在vuex的action主动抛出错误
        await store.dispatch('user/userMobileLogin', user)
        Message({ type: 'success', text: '登录成功' })
        router.push('/')
      } catch (err) {
        console.dir('错误对象')
        console.dir(err)
        Message({ type: 'error', text: err.message })
      }
    }
    const { time, start } = useCountdown()
    return { isMsgLogin, formData, schema, target, login, password, send, time, start }
  }
}
</script>
<style lang="less" scoped>
// 账号容器
.account-box {
  .toggle {
    padding: 15px 40px;
    text-align: right;
    a {
      color: @xtxColor;
      i {
        font-size: 14px;
      }
    }
  }
  .form {
    padding: 0 40px;
    &-item {
      margin-bottom: 28px;
      .input {
        position: relative;
        height: 36px;
        > i {
          width: 34px;
          height: 34px;
          background: #cfcdcd;
          color: #fff;
          position: absolute;
          left: 1px;
          top: 1px;
          text-align: center;
          line-height: 34px;
          font-size: 18px;
        }
        input {
          padding-left: 44px;
          border: 1px solid #cfcdcd;
          height: 36px;
          line-height: 36px;
          width: 100%;
          &.error {
            border-color: @priceColor;
          }
          &.active,&:focus {
            border-color: @xtxColor;
          }
        }
        .code {
          position: absolute;
          right: 1px;
          top: 1px;
          text-align: center;
          line-height: 34px;
          font-size: 14px;
          background: #f5f5f5;
          color: #666;
          width: 90px;
          height: 34px;
          cursor: pointer;
        }
      }
      > .error {
        position: absolute;
        font-size: 12px;
        line-height: 28px;
        color: @priceColor;
        i {
          font-size: 14px;
          margin-right: 2px;
        }
      }
    }
    .agree {
      a {
        color: #069;
      }
    }
   .btn {
    display: block;
    width: 100%;
    height: 40px;
    color: #fff;
    text-align: center;
    line-height: 40px;
    background: @xtxColor;
    &.disabled {
      background: #cfcdcd;
    }
   }
  }
  .action {
    padding: 20px 40px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .url {
      a {
        color: #999;
        margin-left: 10px;
      }
    }
  }
}
</style>
